package br.brpv.silvestrin.vendas.mainactivity;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import br.brpv.silvestrin.vendas.R;
import br.brpv.silvestrin.vendas.domain.*;
import br.brpv.silvestrin.vendas.listeners.CompanyParamsQueryListener;
import br.brpv.silvestrin.vendas.manager.ClientManager;
import br.brpv.silvestrin.vendas.util.Common;
import br.brpv.silvestrin.vendas.util.ShowMessage;
import br.brpv.silvestrin.vendas.util.TextViewDatePicker;
import br.brpv.silvestrin.vendas.util.TextViewTimePicker;
import br.brpv.silvestrin.vendas.webservice.ServerConnection;
import br.brpv.silvestrin.vendas.webservice.ServerDataCacheService;

import java.util.ArrayList;

public class FinalizeOrderScreen implements CompanyParamsQueryListener, ClientManager.ClientManagerParamsInterface {

    private Button sendOrderButton;
    private Button backButton;
    private Button saveButton;
    private EditText obsBuy;

    private EstabParamsDefault estabParamsDefault;

    private enum DiscountType {PERCENT, VALUE}
    private enum Click {SAVE, SEND};
    private Click buttonClicked;

    private EditText cpfEdit;
    private EditText nameEdit;
    private AutoCompleteTextView condPayment;
    private AutoCompleteTextView bankText;
    private AutoCompleteTextView invoiceText;
    private AutoCompleteTextView vendorText;
    private AutoCompleteTextView transpText;
    private AutoCompleteTextView motoristText;
    private AutoCompleteTextView addressText;
    private AutoCompleteTextView placaText;

    private TextViewDatePicker entregaDatePicker;
    private TextViewTimePicker entregaTimeBeginPicker;
    private TextViewTimePicker entregaTimeEndPicker;

    private TextViewDatePicker expedicaoDatePicker;
    private TextViewTimePicker expedicaoTimePicker;

    private RadioButton freteTpFob;
    private RadioButton freteTpCif;

    private EditText discountEdit;

    private ToggleButton discountTypeButton;

    private EditText valueFrete;
    private EditText obsOrder;
    private EditText obsInvoice;
    private EditText valorBrutoEdit;
    private EditText valorLiquidoEdit;

    private LinearLayout finalConsumerFields;

    private ArrayAdapter<Transportation> adapterTransportation = null;
    private ArrayAdapter<PaymentForm> adapterPaymentForm = null;
    private ArrayAdapter<Bank> adapterBanks = null;
    private ArrayAdapter<Invoice> adapterInvoice = null;
    private ArrayAdapter<Vendor> adapterVendor = null;
    private ArrayAdapter<Motorist> adapterMotorist = null;
    private ArrayAdapter<Placa> adapterPlaca = null;
    private ArrayAdapter<Address> adapterAddress = null;

    private ArrayList<Transportation> transportationArrayList = null;
    private ArrayList<PaymentForm> paymentFormArrayList = null;
    private ArrayList<Bank> bankArrayList = null;
    private ArrayList<Invoice> invoiceArrayList = null;
    private ArrayList<Vendor> vendorArrayList = null;
    private ArrayList<Motorist> motoristArrayList = null;

    private LinearLayout finalFormView;
    private Activity mActivity;
    private int codSelectedCompany;

    private FinalizeOrderInterface finalizeOrderInterface;

    private CompanyParameters paramsSelectedCompany;
    private String establishmentID = null;
    private String clientID = null;
    private String clientCode;
    private Order currentOrder = null;
    private ClientManager clientManager;

    private ServerDataCacheService mServerDataCacheBinder;

    public FinalizeOrderScreen(Activity activity, FinalizeOrderInterface finalizeOrderInterface){
        mActivity = activity;
        clientManager = new ClientManager(activity, null, this);
        this.finalizeOrderInterface = finalizeOrderInterface;
        adapterTransportation = new ArrayAdapter<Transportation>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Transportation>());
        adapterPaymentForm = new ArrayAdapter<PaymentForm>(mActivity, android.R.layout.simple_dropdown_item_1line,new ArrayList<PaymentForm>());
        adapterBanks = new ArrayAdapter<Bank>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Bank>());
        adapterInvoice = new ArrayAdapter<Invoice>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Invoice>());
        adapterVendor = new ArrayAdapter<Vendor>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Vendor>());
        adapterMotorist = new ArrayAdapter<Motorist>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Motorist>());
        adapterPlaca = new ArrayAdapter<Placa>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Placa>());
        adapterAddress = new ArrayAdapter<Address>(mActivity,android.R.layout.simple_dropdown_item_1line, new ArrayList<Address>());
    }

    public void initFinalizeOrderScreen(){

        if(finalFormView != null) {

            bankText = (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_bank);
            transpText = (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_transportation);
            invoiceText = (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_invoice_type);
            vendorText = (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_vendor);
            motoristText = (AutoCompleteTextView)finalFormView.findViewById(R.id.confirm_order_motorist);
            condPayment = (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_cond_payment);
            addressText = (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_address);
            placaText =  (AutoCompleteTextView) finalFormView.findViewById(R.id.confirm_order_placa);

            entregaDatePicker = (TextViewDatePicker) finalFormView.findViewById(R.id.confirm_data_entrega_picker);
            entregaTimeBeginPicker = (TextViewTimePicker) finalFormView.findViewById(R.id.confirm_hora_inicio_entrega_picker);

            entregaTimeEndPicker = (TextViewTimePicker) finalFormView.findViewById(R.id.confirm_hora_fim_entrega_picker);

            expedicaoDatePicker = (TextViewDatePicker) finalFormView.findViewById(R.id.lim_expedicao_date_picker);
            expedicaoTimePicker = (TextViewTimePicker) finalFormView.findViewById(R.id.lim_expedicao_time_picker);

            freteTpFob = (RadioButton) finalFormView.findViewById(R.id.confirm_order_frete_fob);
            freteTpCif = (RadioButton) finalFormView.findViewById(R.id.confirm_order_frete_cif);
            discountTypeButton = (ToggleButton) finalFormView.findViewById(R.id.confirm_discount_type);
            discountEdit = (EditText) finalFormView.findViewById(R.id.confirm_order_discount_value);

            valueFrete = (EditText) finalFormView.findViewById(R.id.confirm_valor_frete);
            valorBrutoEdit = (EditText) finalFormView.findViewById(R.id.confirm_order_total_brt_value);
            valorLiquidoEdit = (EditText) finalFormView.findViewById(R.id.confirm_order_total_lqd);

            obsOrder = (EditText)finalFormView.findViewById(R.id.confirm_obs_order);
            obsInvoice = (EditText)finalFormView.findViewById(R.id.confirm_obs_invoice);
            obsBuy = (EditText)finalFormView.findViewById(R.id.confirm_obs_compra);

            sendOrderButton = (Button) finalFormView.findViewById(R.id.confirm_order_send_button);

            finalConsumerFields = (LinearLayout)finalFormView.findViewById(R.id.confirm_order_final_consumer_fields);
            cpfEdit = (EditText)finalFormView.findViewById(R.id.confirm_order_cpf);
            nameEdit = (EditText)finalFormView.findViewById(R.id.confirm_order_name);
        }

    }

    public void showEndOrder(){
        System.out.println("Show EndOrder");
        finalFormView.setVisibility(View.VISIBLE);
        initEndOrderAutocompleteText();
    }

    public void setVisible(){
        if(finalFormView!=null) finalFormView.setVisibility(View.VISIBLE);
        System.out.println("Visible");
    }
    public void setInvisible(){
        if(finalFormView!=null) finalFormView.setVisibility(View.INVISIBLE);
        System.out.println("Invisible");
    }

    /*******Getters and Setters******/
    public void setmServerDataCacheBinder(ServerDataCacheService mServerDataCacheBinder) {
        this.mServerDataCacheBinder = mServerDataCacheBinder;
        clientManager.setmServerDataCacheBinder(mServerDataCacheBinder);
    }

    public void setFinalFormView(LinearLayout finalFormView) {
        this.finalFormView = finalFormView;
    }

    public void setCodSelectedCompany(int codSelectedCompany) {
        if(this.codSelectedCompany != codSelectedCompany) {
            this.codSelectedCompany = codSelectedCompany;
            clientManager.setCodSelectedCompany(codSelectedCompany);
            getCompanyParams();
        }
    }


    public void setClientAndEstablishment(String clientCode, String clientID, String establishmentID, EstabParamsDefault estabParamsDefault){
        sendOrderButton.setEnabled(false);

        this.clientCode = clientCode;
        this.clientID = clientID;
        clientManager.getClientAddress(clientID);
        clientManager.getClientPlacas(clientID);

        this.establishmentID = establishmentID;
        //clientManager.getClientDefaultParams(clientID, establishmentID);
        initWithDefaultParams(estabParamsDefault);

    }

    public void setParamsSelectedCompany(CompanyParameters paramsSelectedCompany) {
        this.paramsSelectedCompany = paramsSelectedCompany;
    }

    public void setOrder(Order currentOrder) {
        this.currentOrder = currentOrder;
    }

    public void setValorLiquido(String valorLiquido){
        currentOrder.setValorBruto(valorLiquido);
        valorLiquidoEdit.setText(String.valueOf(valorLiquido));
    }
    public void setValorBruto(String valorBruto){
        currentOrder.setValorBruto(valorBruto);
        valorBrutoEdit.setText(String.valueOf(valorBruto));
    }

    /********************************/

    private void initWithDefaultParams(EstabParamsDefault estabParamsDefault) {
        this.estabParamsDefault = estabParamsDefault;

        initTransportationWithDefault();

        initBankWithDefault();

        initInvoiceWithDefault();

        initVendrWithDefault();

        initMotoristWithDefault();

        initPaymentWithDefault();

        if(paramsSelectedCompany.getTransporterType().contentEquals("F")) freteTpFob.setChecked(true);
        else freteTpCif.setChecked(true);

        entregaTimeBeginPicker.setTime(8, 0);
        entregaTimeEndPicker.setTime(18, 0);
        expedicaoTimePicker.setTime(8, 0);

        placaText.setText("");
        addressText.setText("");
        obsInvoice.setText("");
        obsOrder.setText("");
        obsBuy.setText("");
        valueFrete.setText("0");

        showFinalConsumerFieldsIfNeeded();

        cpfEdit.setText("");
        nameEdit.setText("");

        sendOrderButton.setEnabled(true);
    }

    private void initPaymentWithDefault() {
        condPayment.setText("");
        if(estabParamsDefault != null) {
            PaymentForm paymentForm = (PaymentForm) findOrderParamById(paymentFormArrayList, estabParamsDefault.getPaymentTypeId());
            if(paymentForm != null) condPayment.setText(paymentForm.getText());
        }
    }

    private void initMotoristWithDefault() {
        motoristText.setText("");
        if(estabParamsDefault != null) {
            Motorist motorist = (Motorist) findOrderParamById(motoristArrayList, estabParamsDefault.getTprvenId());
            if(motorist != null) motoristText.setText(motorist.getName());
        }
    }

    private void initVendrWithDefault() {
        vendorText.setText("");
        if(estabParamsDefault != null) {
            Vendor vendor = findOrderParamById(vendorArrayList, estabParamsDefault.getVendorId());
            if(vendor != null) vendorText.setText(vendor.getName());
        }
    }


    private void initInvoiceWithDefault() {
        invoiceText.setText("");
        if(estabParamsDefault != null) {
            Invoice invoice = (Invoice) findOrderParamById(invoiceArrayList, estabParamsDefault.getInvoiceId());
            if(invoice != null) invoiceText.setText(invoice.getDescription());
        }
    }

    private void initBankWithDefault() {
        bankText.setText("");
        if(estabParamsDefault != null) {
            Bank bank = findOrderParamById(bankArrayList, estabParamsDefault.getBankId());
            if(bank != null) bankText.setText(bank.getText());
        }
    }

    private void initTransportationWithDefault() {
        transpText.setText("");

        if(estabParamsDefault != null) {
            Transportation defaultTrans = findOrderParamById(transportationArrayList,
                    estabParamsDefault.getFornId());
            if(defaultTrans != null) transpText.setText(defaultTrans.getName());
        }
    }

    public void showFinalConsumerFieldsIfNeeded() {
        if(clientCode == null || paramsSelectedCompany == null || finalConsumerFields == null) return;

        if(clientCode.equals(paramsSelectedCompany.getCodFinalConsumer())) {
            finalConsumerFields.setVisibility(View.VISIBLE);
        } else {
            finalConsumerFields.setVisibility(View.GONE);
        }
    }

    private void setParamsToOrder() {
         PaymentForm paymentForm = (PaymentForm) findOrderParamByText(adapterPaymentForm, condPayment.getText().toString());
        if(paymentForm != null) currentOrder.setCondPaymentId(paymentForm.getId());

        Bank bank = (Bank) findOrderParamByText(adapterBanks, bankText.getText().toString());
        if(bank != null) currentOrder.setPor_id(bank.getId());

        Invoice invoice = (Invoice) findOrderParamByText(adapterInvoice, invoiceText.getText().toString());
        if(invoice != null) currentOrder.setTypeNotaFiscal(invoice.getId());

        Vendor vendor = (Vendor) findOrderParamByText(adapterVendor, vendorText.getText().toString());
        if(vendor != null) currentOrder.setRepresentanteId(vendor.getId());

        Transportation transportation = (Transportation) findOrderParamByText(adapterTransportation, transpText.getText().toString());
        if(transportation != null) currentOrder.setForn_id(transportation.getId());

        Motorist motorist = (Motorist) findOrderParamByText(adapterMotorist, motoristText.getText().toString());
        if(motorist != null) currentOrder.setDriverId(motorist.getId());

        currentOrder.setDateDeliver(entregaDatePicker.getDate());
        currentOrder.setHourInicial(entregaTimeBeginPicker.getTime());
        currentOrder.setHourFinal(entregaTimeEndPicker.getTime());

        currentOrder.setDateExped(expedicaoDatePicker.getDate());
        currentOrder.setHourExped(expedicaoTimePicker.getTime());

        String disc = discountEdit.getText().toString();
        if(disc.length() == 0) disc = "0";
        currentOrder.setValueDiscount(disc);

        if(discountTypeButton.isChecked()) currentOrder.setTypeDiscount("V");
        else currentOrder.setTypeDiscount("P");

        if(freteTpCif.isChecked()) currentOrder.setTypeFrete("C");
        if(freteTpFob.isChecked()) currentOrder.setTypeFrete("F");

        currentOrder.setValueFrete(valueFrete.getText().toString());

        currentOrder.setObsOrder(obsOrder.getText().toString());
        currentOrder.setObsNotaFiscal(obsInvoice.getText().toString());

        currentOrder.setName(nameEdit.getText().toString());
        if(cpfEdit.getText().toString().isEmpty()) {
            currentOrder.setCPF("0");
        } else {
            currentOrder.setCPF(cpfEdit.getText().toString());
        }

    }


    private void initEndOrderAutocompleteText() {
        System.out.println("CALL - initEndOrderAutocompleteText");
        condPayment.setThreshold(0);
        condPayment.setAdapter(adapterPaymentForm);
        condPayment.setText("");

        bankText.setThreshold(0);
        bankText.setAdapter(adapterBanks);
        bankText.setText("");

        invoiceText.setThreshold(0);
        invoiceText.setAdapter(adapterInvoice);
        invoiceText.setText("");

        vendorText.setThreshold(0);
        vendorText.setAdapter(adapterVendor);
        vendorText.setText("");

        transpText.setThreshold(0);
        transpText.setAdapter(adapterTransportation);
        transpText.setText("");

        motoristText.setThreshold(0);
        motoristText.setAdapter(adapterMotorist);
        motoristText.setText("");

        addressText.setThreshold(0);
        addressText.setAdapter(adapterAddress);
        addressText.setText("");
        
        placaText.setThreshold(0);
        placaText.setAdapter(adapterPlaca);
        placaText.setText("");

        discountEdit.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                setConfirmOrderValues();
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        if(discountTypeButton != null) discountEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setConfirmOrderValues();
            }
        });

        sendOrderButton = (Button) finalFormView.findViewById(R.id.confirm_order_send_button);
        sendOrderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonClicked = Click.SEND;
                setAddressAndPlaca();
            }
        });

        backButton = (Button) finalFormView.findViewById(R.id.confirm_order_back_button);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finalizeOrderInterface.backButtonClick(currentOrder);
            }
        });

        saveButton = (Button)finalFormView.findViewById(R.id.confirm_order_save_button);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonClicked = Click.SAVE;
                setAddressAndPlaca();
            }
        });
    }

    private void sendOrder(){
        if(!areAllParamsRight()) return;

        setParamsToOrder();

        if(buttonClicked == Click.SEND){
            finalizeOrderInterface.finalizeOrder(currentOrder);
        } else {
            finalizeOrderInterface.saveOrder(currentOrder);
        }
    }

    private boolean areAllParamsRight() {

        //check if CPF is OK
        if(!cpfEdit.getText().toString().isEmpty()) {
            String cpf = cpfEdit.getText().toString();
            if(!Common.isCPF(cpf)) {
                ShowMessage.alertDialog(mActivity, R.string.message_invalid_cpf);
                return false;
            }
        }

        return true;
    }

    private void setAddressToOrder(){
        String text;

        text =  addressText.getText().toString();
        if(text.length() > 0){
            Address address = (Address) findOrderParamByText(adapterAddress, text);
            if(address != null) {
                currentOrder.setAddressId(address.getId());
                setPlacaToOrder();
            } else {
                clientManager.setClientAddress(clientID, text);
            }
        }
    }

    private void setPlacaToOrder(){
        String text;

        text = placaText.getText().toString();
        if(text.length() > 0){
            Placa placa = (Placa) findOrderParamByText(adapterAddress, text);
            if(placa != null){
                currentOrder.setPlacaId(placa.getId());
                sendOrder();
            } else {
                clientManager.setClientPlaca(clientID, text);
            }
        } else {
            sendOrder();
        }
    }

    private void setAddressAndPlaca(){
        String text;

        text =  addressText.getText().toString();
        if(text.length() > 0){
            setAddressToOrder();
        }else {
            setPlacaToOrder();
        }
    }

    private <E extends OrderParam> E findOrderParamById(ArrayList<E> array, String id){
        E ret = null;
        E aux;
        if(array != null) {
            for(int i = 0;i < array.size(); i++){
                aux =  (E)array.get(i);
                if(aux.getId().equals(id)){
                    ret = aux;
                    break;
                }
            }
        } else {
            System.out.println("ADAPTER NULL");
        }
        return ret;
    }

    private OrderParam findOrderParamByText(ArrayAdapter<?> array, String text){
        OrderParam ret = null;
        OrderParam aux;
        if(array != null){
            for(int i = 0;i < array.getCount(); i++){
                aux =  (OrderParam)array.getItem(i);
                if(aux.getText().equals(text)){
                    ret = aux;
                    break;
                }
            }
        }
        return ret;
    }

    private void setConfirmOrderValues() {
        double value = 0;

        DiscountType discountType;
        if(discountTypeButton.isChecked()) discountType = DiscountType.VALUE;
        else discountType = DiscountType.PERCENT;

        String discountValue = discountEdit.getText().toString();
        if(discountValue.length() == 0) discountValue = "0";

        if(currentOrder != null){
            switch (discountType){
                case VALUE:
                    value = Double.parseDouble(currentOrder.getValorBruto()) - Double.parseDouble(discountValue);
                    break;
                case PERCENT:
                    value = Double.parseDouble(currentOrder.getValorBruto()) *
                            (100 - Double.parseDouble(discountValue))/100;
                    break;
            }

            currentOrder.setValorLiquido(String.valueOf(value));
            EditText valorLiquido = (EditText) finalFormView.findViewById(R.id.confirm_order_total_lqd);
            valorLiquido.setText(String.valueOf(value));
        }
    }

    private void getCompanyParams(){
        System.out.println("CALL - getCompanyParams");
        CompanyParameters compParams = null;

        if(mServerDataCacheBinder != null)
            transportationArrayList = mServerDataCacheBinder.getCompanyTransportation(codSelectedCompany);
        if(transportationArrayList == null)
            ServerConnection.getCompanyTransportation(codSelectedCompany, FinalizeOrderScreen.this);
        else {
            adapterTransportation.clear();
            adapterTransportation.addAll(transportationArrayList);
        }

        if(mServerDataCacheBinder != null)
            paymentFormArrayList = mServerDataCacheBinder.getCompanyPaymentForm(codSelectedCompany);
        if(paymentFormArrayList == null)
            ServerConnection.getCompanyCondPayment(codSelectedCompany, FinalizeOrderScreen.this);
        else {
            adapterPaymentForm.clear();
            adapterPaymentForm.addAll(paymentFormArrayList);
        }

        if(mServerDataCacheBinder != null)
            bankArrayList = mServerDataCacheBinder.getBanksList();
        if(bankArrayList == null || bankArrayList.size() == 0)
            ServerConnection.getPortadores(FinalizeOrderScreen.this);
        else {
            adapterBanks.clear();
            adapterBanks.addAll(bankArrayList);
        }

        if(mServerDataCacheBinder != null)
            invoiceArrayList = mServerDataCacheBinder.getCompanyInvoiveTypes(codSelectedCompany);
        if(invoiceArrayList == null || invoiceArrayList.size() == 0)
            ServerConnection.getCompanyInvoiceType(codSelectedCompany, FinalizeOrderScreen.this);
        else {
            adapterInvoice.clear();
            adapterInvoice.addAll(invoiceArrayList);
        }

        if(mServerDataCacheBinder != null)
            vendorArrayList = mServerDataCacheBinder.getCompanyVendors(codSelectedCompany);
        if(vendorArrayList == null || vendorArrayList.size() == 0)
            ServerConnection.getCompanyVendors(codSelectedCompany, FinalizeOrderScreen.this);
        else {
            adapterVendor.clear();
            adapterVendor.addAll(vendorArrayList);
        }

        ArrayList<Motorist> motoristArrayList = null;
        if(mServerDataCacheBinder != null)
            motoristArrayList = mServerDataCacheBinder.getMotoristList();
        if(motoristArrayList == null || motoristArrayList.size() == 0)
            ServerConnection.getMotorists(FinalizeOrderScreen.this);
        else {
            adapterMotorist.clear();
            adapterMotorist.addAll(motoristArrayList);
        }

    }


    /******Web Service Callbacks******/
    @Override
    public void OnWebServiceCompanyTransportationReturn(ArrayList<Transportation> transportList) {
        transportationArrayList = transportList;
        //adiciona na cache
        mServerDataCacheBinder.addCompanyTransportation(codSelectedCompany, transportList);
        adapterTransportation = new ArrayAdapter<Transportation>(getActivity(), android.R.layout.simple_dropdown_item_1line,
                transportList);

        initTransportationWithDefault();
    }

    @Override
    public void failedOnRetrieveTransportation() {
    }

    @Override
    public void failedOnRetrievePaymentForm() {

    }

    @Override
    public Context getActivity() {
        return mActivity;
    }

    @Override
    public void OnWebServiceBanksReturn(ArrayList<Bank> banksList) {
        bankArrayList = banksList;
        //adiciona na cache
        mServerDataCacheBinder.setBanksList(banksList);

        adapterBanks.clear();
        adapterBanks.addAll(banksList);

        initBankWithDefault();
    }

    @Override
    public void failedOnRetrieveBanks() {
    }

    @Override
    public void OnWebServiceInvoiceTypeReturn(ArrayList<Invoice> invoiceArrayList) {
        this.invoiceArrayList = invoiceArrayList;
        //adiciona na cache
        mServerDataCacheBinder.addCompanyInvoiceTypes(codSelectedCompany, invoiceArrayList);
        adapterInvoice.clear();
        adapterInvoice.addAll(invoiceArrayList);

        initInvoiceWithDefault();
    }

    @Override
    public void failedOnRetrieveInvoiceType() {
    }

    @Override
    public void failedOnRetriveVendors() {
    }

    @Override
    public void OnWebServiceVendorsReturn(ArrayList<Vendor> vendorArrayList) {
        this.vendorArrayList = vendorArrayList;
        //adiciona na cache
        mServerDataCacheBinder.addCompanyVendors(codSelectedCompany, vendorArrayList);
        adapterVendor.clear();
        adapterVendor.addAll(vendorArrayList);

        initVendrWithDefault();

    }

    @Override
    public void failedOnRetriveMotorists() {
    }

    @Override
    public void OnWebServiceMotoristsReturn(ArrayList<Motorist> motoristArrayList) {
        this.motoristArrayList = motoristArrayList;
        //adiciona na cache
        mServerDataCacheBinder.setMotoristList(motoristArrayList);
        adapterMotorist.clear();
        adapterMotorist.addAll(motoristArrayList);

        initMotoristWithDefault();
    }


    @Override
    public void OnWebServiceCompanyPaymentFormReturn(ArrayList<PaymentForm> paymentFormArrayList) {
        this.paymentFormArrayList = paymentFormArrayList;
        //adiciona na cache
        mServerDataCacheBinder.addCompanyPaymenForm(codSelectedCompany, paymentFormArrayList);
        adapterPaymentForm.clear();
        adapterPaymentForm.addAll(paymentFormArrayList);

        initPaymentWithDefault();
    }

    @Override
    public void onClientPlacasReturn(ArrayList<Placa> placas) {
        //adapterPlaca = new ArrayAdapter<Placa>(mActivity,android.R.layout.simple_dropdown_item_1line,placas);
        adapterPlaca.clear();
        adapterPlaca.addAll(placas);
        if(placaText != null) placaText.setAdapter(adapterPlaca);
    }

    @Override
    public void onClientAddressReturn(ArrayList<Address> addresses) {
        //adapterAddress = new ArrayAdapter<Address>(mActivity,android.R.layout.simple_dropdown_item_1line,addresses);
        adapterAddress.clear();
        adapterAddress.addAll(addresses);
        if(addressText != null) addressText.setAdapter(adapterAddress);
    }

    @Override
    public void onClientSetPlacaReturn(String id) {
        System.out.println("Placa :" + id);
        currentOrder.setPlacaId(id);
        sendOrder();
    }

    @Override
    public void onClientSetAddressReturn(String id) {
        System.out.println("Endereco " + id);
        currentOrder.setAddressId(id);
        setPlacaToOrder();
    }

    /*********************************/

}
